# Copyright (c) 2022 Presto Labs Pte. Ltd.
# Author: daniel

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


trade_symbol = ['BNB-BUSD', 'BNB-USDT', 'CAKE-BNB']


def gen_customized_native_symbol():
  symbols = dict()
  symbols['BNB-BUSD'] = {
    "base_symbol": "BNB",
    "quote_symbol": "BUSD",
  }
  symbols['BNB-USDT'] = {
    "base_symbol": "BNB",
    "quote_symbol": "USDT",
  }
  symbols['CAKE-BNB'] = {
    "base_symbol": "CAKE",
    "quote_symbol": "BNB",
  }
  return symbols


def gen_var_price_ticks(native_symbol):
  price_list = [1e9, 0]
  tick_list = [0.01]

  range_list = list(zip(price_list[:-1], price_list[1:]))
  result = []
  for r, tick in zip(range_list, tick_list):
    assert r[0] > r[1]
    info = {
        'from': r[1],
        'to': r[0],
        'value': 0.0000001 if native_symbol == 'CAKE-BNB' else tick,
    }
    result.append([info['from'], info['to'], info['value']])
  return result


def gen_var_qty_ticks(native_symbol):
  qty_ticksize = 0.001
  return qty_ticksize


def unpack_wrapper_currency(native_currency):
  if 'W' == native_currency[0]:
    return native_currency[1:]
  else:
    return native_currency


def generate():
  mea = 'Spot.Pancakeswap.v2'
  me = 'Spot.Pancakeswap'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  # data = requests.get('https://api.pancakeswap.info/api/v2/pairs').json()['data']
  data = gen_customized_native_symbol()

  pis = []
  for info in data.values():
    native_base = unpack_wrapper_currency(info['base_symbol'])
    native_quote = unpack_wrapper_currency(info['quote_symbol'])
    native_symbol = f"%s-%s" % (native_base, native_quote)
    if native_symbol not in trade_symbol:
      continue
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = f"%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        is_inverse=False,
        price_ticksize=gen_var_price_ticks(native_symbol),
        qty_ticksize=gen_var_qty_ticks(native_symbol),
        min_qty=float(0.01),
        min_amount=float(10),
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  base_currency_list = [info['native_base'] for info in data]
  res = {
      'coin_list': list(set(base_currency_list)),
  }
  return res


if __name__ == '__main__':
  import json
  config = generate()
  print(json.dumps(config, indent=2))
